<template>
  <div>
     <el-card>
         <div class="search_box">
      <el-row :gutter="20">
        <el-form size="mini" :model="searchForm" label-width="80px">
          <el-col :span="5">
            <el-form-item label="用户名">
              <el-input
                clearable
                  @clear="handleSearch"
                @keyup.enter.native="handleSearch"
                v-model="searchForm.username"
                placeholder="请输入用户名"
                auto-complete="false"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="ip地址">
              <el-input
                @clear="handleSearch"
                @keyup.enter.native="handleSearch"
                v-model="searchForm.ip"
                placeholder="请输入ip地址"
                auto-complete="false"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="登入地址">
              <el-input
                @clear="handleSearch"
                @keyup.enter.native="handleSearch"
                v-model="searchForm.location"
                placeholder="请输入登入地址"
                auto-complete="false"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleSearch"
              >查询</el-button
            >
            <el-button icon="el-icon-delete" @click="handleReset"
              >重置</el-button
            >
            <el-button icon="el-icon-delete" v-permission="'loginLog:batchDelete'" :disabled='delDisabled' @click="handleDels">删除</el-button>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div class="table_box">
      <el-table size="small" :data="logList" border stripe   @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column align="center" width="80" show-overflow-tooltip type="index" label="#">
        </el-table-column>
        <el-table-column align="center" prop="username" label="登入用户">
        </el-table-column>
        <el-table-column align="center" prop="loginTime" label="登入时间">
        </el-table-column>
        <el-table-column align="center" prop="location" label="登入地点">
        </el-table-column>
        <el-table-column align="center" prop="ip" label="IP地址">
        </el-table-column>
        <el-table-column align="center" prop="userSystem" label="操作系统">
        </el-table-column>
        <el-table-column align="center" prop="userBrowser" label="浏览器">
        </el-table-column>
        <el-table-column align="center" prop="username" label="操作">
            <template scope="scope">
                <el-button type="text" v-permission="'loginLog:delete'" @click="del(scope.row.id)" icon="el-icon-delete">删除</el-button>
            </template>
        </el-table-column>
      </el-table>
    </div>
    <pageTools
      :total="total"
      :paginationPage="searchForm.pageNum"
      :paginationPagesize="searchForm.pageSize"
      @pageChange="pageChange"
      @pageSizeChange="pageSizeChange"
    ></pageTools>
     </el-card>
  </div>
</template>

<script>
import { getlogListApi, dellogApi, delslogApi } from '../../api/loginlog'
export default {
  name: 'loginLog',
  data () {
    return {
      searchForm: {
        ip: '',
        username: '',
        location: '',
        pageNum: 1,
        pageSize: 5
      },
      total: 0,
      logList: [],
      checkedList: []
    }
  },
  methods: {
    handleDels () {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.onDeletes()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async onDeletes () {
      const res = await delslogApi(this.checkedList)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.getLogList()
      this.$notify.success('删除成功')
    },
    handleSelectionChange (val) {
      this.checkedList = val.map(item => item.id)
    },
    del (id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.onDelete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async onDelete (id) {
      const res = await dellogApi(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.getLogList()
      this.$notify.success('删除成功')
    },
    handleReset () {
      this.searchForm = {
        ip: '',
        username: '',
        location: '',
        pageNum: 1,
        pageSize: 5
      }
    },
    handleSearch () {
      this.searchForm.pageNum = 1
      this.getLogList()
    },
    async getLogList () {
      const res = await getlogListApi(this.searchForm)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.total = res.data.total
      this.logList = res.data.rows
    },
    pageChange (val) {
      this.searchForm.pageNum = val
      this.getLogList()
    },
    pageSizeChange (val) {
      this.searchForm.pageSize = val
      this.getLogList()
    }
  },
  created () {
    this.getLogList()
  },
  computed: {
    delDisabled () {
      if (this.checkedList.length === 0) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style>
</style>
